package com.b2.controller.web;

import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.b2.pojo.entity.AliOss;
import com.b2.result.Result;
import com.b2.service.AbilityService;
import com.b2.utils.AliOssUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.SecureRandom;

@RestController
@Api(tags = "通用接口")
@RequestMapping("/common")
@Slf4j
@CrossOrigin
public class CommonController {

    @Resource
    private AliOssUtil aliOssUtil;

    @Autowired
    AbilityService abilityService;

    @PostMapping("/upload")
    @ApiOperation("上传封面图片")
    public Result<String> upload(@ApiParam("封面图片")@RequestParam("file") MultipartFile file) {
        String fileUrl = null;
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String finalFileName = "cover/" + System.currentTimeMillis() + "" + new SecureRandom().nextInt(0x0400) + suffixName;
        try {
            fileUrl = aliOssUtil.upload(file.getBytes(), finalFileName);
        } catch (IOException e) {
            log.info("文件上传失败",e);
        }
        return Result.success(fileUrl);
    }
    @ApiOperation("能力介绍")
    @PostMapping("/getInformation")
    public  Result<String[]> getInformation(@ApiParam("能力编码")@RequestBody String[] abilityCodes){
       String[] informations= abilityService.getInformation(abilityCodes);
       return Result.success(informations);
    }

    @ApiOperation("获取绘本名字")
    @GetMapping("/getname")
    public  Result<String> getname(@ApiParam("能力编码")String abilitycode){
        String name= abilityService.getname(abilitycode);
        return Result.success(name);
    }
}
